// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LivingDracula
//@version=5
indicator("Money Flow Index MTF + Alerts with Candle Opacity & Labels", overlay=true)

import livingdracula/QTA/2 as QTA
patternLabelPosLow = low - ta.atr(30) * 0.6
patternLabelPosHigh = high + ta.atr(30) * 0.6

////////////////////////////////////////////////////////////////////}
////# INPUTS
////////////////////////////////////////////////////////////////////{
_group_MFI = "MFI Settings"
useMFI                      = input.bool(defval=true, title='MFI Candles',         group=_group_MFI, inline='00')
useMFI_labels               = input.bool(defval=true, title='MFI Labels',          group=_group_MFI, inline='00')
length                      = input.int(title="Length", defval=14)
os                          = input.int(20, title="Oversold")
ob                          = input.int(80, title="Overbought")
CurrentRes                  = input.bool(true, title="Use Current Chart Resolution?")
CustomRes                   = input.timeframe("240", title="Custom Timeframe? Uncheck Box Above (E.g. 1M, 5D, 240 = 4Hours)")



////////////////////////////////////////////////////////////////////}
////# MFI CALCULATION
////////////////////////////////////////////////////////////////////{
rawMoneyFlow = hlc3 * volume
// Use the current chart resolution or a custom resolution
res = CurrentRes ? timeframe.period : CustomRes


// Calculate positive and negative money flows:
// (If the typical price increased from the previous bar, use the raw money flow; otherwise, 0.)
posFlow = hlc3 > hlc3[1] ? rawMoneyFlow : 0.0
negFlow = hlc3 < hlc3[1] ? rawMoneyFlow : 0.0

// Compute the money flow ratio and the MFI on the desired resolution.
moneyFlowRatio = ta.sma(posFlow, length) / ta.sma(negFlow, length)
localMFI       = 100 - 100 / (1 + moneyFlowRatio)
moneyFlowIndex = request.security(syminfo.tickerid, res, localMFI)

////////////////////////////////////////////////////////////////////}
////# OVERBOUGHT / OVERSOLD IDENTIFICATION
////////////////////////////////////////////////////////////////////{
oversold   = (moneyFlowIndex[1] > os) and (moneyFlowIndex < os)
overbought = (moneyFlowIndex[1] < ob) and (moneyFlowIndex > ob)

////////////////////////////////////////////////////////////////////}
////# CANDLE OPACITY BASED ON MONEY FLOW INDEX
////////////////////////////////////////////////////////////////////{
candleTransp = 100 - math.round(moneyFlowIndex)
barCol = close >= open ? color.new(#9698a1, candleTransp) : color.new(#ff0303, candleTransp)
barcolor(useMFI ? barCol : na)

////////////////////////////////////////////////////////////////////}
////# LABELS FOR OVERBOUGHT/OVERSOLD CONDITIONS
////////////////////////////////////////////////////////////////////{
if useMFI_labels and overbought
    label.new(bar_index, patternLabelPosHigh, "Overbought", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.tiny)
if useMFI_labels and oversold
    label.new(bar_index, patternLabelPosLow, "Oversold", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.tiny)


////////////////////////////////////////////////////////////////////}
////# ALERT CONDITIONS
////////////////////////////////////////////////////////////////////{
alertcondition(oversold, title="MFI Oversold", message="MFI Crossed Oversold")
alertcondition(overbought, title="MFI Overbought", message="MFI Crossed Overbought")
alertcondition(oversold or overbought, title="MFI Alert Both", message="MFI Alert Generated OB/OS")